package com.CusterRelSys.control;

import com.CusterRelSys.entity.CstCustomer;
import com.CusterRelSys.entity.CstService;
import com.CusterRelSys.entity.SysUser;
import com.CusterRelSys.service.CusServiceServiceZS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/CusService")
public class CusServiceControllerZS {
    @Autowired
    CusServiceServiceZS cusServiceServiceZS;

    @RequestMapping("/setCstNo")
    public @ResponseBody Integer setCstNo(){
        List<Integer> cstNoList=cusServiceServiceZS.selectCstNo();
        Integer num=0;
        if(cstNoList!=null) {
            for (Integer cnl : cstNoList) {
                if (cnl > num) {
                    num = cnl;
                }
            }
        }
        return num+1;
    }

    @RequestMapping("/selectAllUser")
    public @ResponseBody List<SysUser> selectAllUser(){
        List<SysUser> userList=cusServiceServiceZS.selectAllUser();
        return userList;
    }

    @RequestMapping(value = "/selectCustomer")
    public @ResponseBody String  selectCustomer(String customeInfo, HttpServletResponse response, HttpServletRequest request){
        CstCustomer cstCustomer=new CstCustomer();
        int i=0;
        for (i=0;i<customeInfo.length();i++){
            char thisChar=customeInfo.charAt(i);
            if (thisChar>='0' && thisChar<='9'){
                cstCustomer.setCustNo(customeInfo);
                List<CstCustomer> cstCustomerList=cusServiceServiceZS.selectCustomer(cstCustomer);
                if (cstCustomerList.size()>0){
                    return cstCustomerList.get(0).getCustName();
                }else {
                    return "没有此用户！";
                }
            }else if(i==customeInfo.length()-1) {
                cstCustomer.setCustName(customeInfo);
                List<CstCustomer> cstCustomerList=cusServiceServiceZS.selectCustomer(cstCustomer);
                if (cstCustomerList.size()>0){
                    String nums="";
                    for (CstCustomer customer : cstCustomerList) {
                        if (nums.trim().length()>0){
                            nums+=","+customer.getCustNo();
                        }else {
                            nums+=customer.getCustNo();
                        }
                    }
                    return nums;
                }else {
                    return "没有此用户！";
                }
            }
        }
        return null;
    }

    @RequestMapping("/selectService")
    public String selectService(String date1,String date2,String path, CstService cstService, ModelMap modelMap, HttpServletRequest request)
            throws UnsupportedEncodingException, ParseException {
        request.setCharacterEncoding("utf-8");
        System.out.println(cstService);
        System.out.println(date1+","+date2);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if (date1!=null && date1.length()>0) {
            Date date001 = sdf.parse(date1);
            long date01=date001.getTime();
            cstService.setSvrCreateDate(date01);
        }if(date2!=null && date2.length()>0) {
            Date date002 = sdf.parse(date2);
            long date02=date002.getTime();
            cstService.setSvrDueDate(date02);
        }
        List<CstService> cstServiceList=cusServiceServiceZS.selectService(cstService);
        modelMap.addAttribute("cstServiceList",cstServiceList);
        modelMap.addAttribute("cstServiceLast",cstService);
        return "~cust/service/"+path;
    }

    @RequestMapping("/deleteService")
    public void deleteService(String svrId) {
        cusServiceServiceZS.deleteService(svrId);
    }
}
